import MiniProgramApi from "./index.js"
import getInfo from "./getInfo.js"

// 获取 token 和 openId
let openId, token
try {
	const value = uni.getStorageSync('token');
	if (value) {
		token = value;
		console.log(value, 'token value');
	}
} catch (e) {
	console.error(e);
}
try {
	const value = uni.getStorageSync('openId');
	if (value) {
		openId = value;
		console.log(value, 'openIdopenIdopenIdopenIdopenIdopenIdopenId');
	}
} catch (e) {
	console.error(e, 'openIdopenIdopenIdopenIdopenIdopenIdopenId err');
}

// 使用闭包创建请求函数
function request(url, method, data, token) {
	if (token == null) {
		try {
			const value = uni.getStorageSync('token');
			if (value) {
				token = value
			}
		} catch (e) {
			// error
		}

	}
	if (openId == null) {
		try {
			const value = uni.getStorageSync('openId');
			if (value) {
				openId = value
			}
		} catch (e) {
			// error
		}

	}
	console.log(token, openId, 'dskjgfdsjhfgdsjkfgdhshjkfg');
	return new Promise((resolve, reject) => {
		uni.request({
			url,
			method,
			header: {
				Authorization: token, // 修改这里的连接方式
				openId
			},
			data,
			success: res => {
				console.log(res, 'sdkjfgdsfjhgdsfhjg');
				if (res.data.code != 200) {
					uni.showToast({
						icon: "none",
						title: res.data.msg,
						duration: 3000
					})
					// if (res.data.retCode == 204) {
					// 	setTimeout(() => {
					// 		uni.navigateTo({
					// 			url: "/pages/showGetPhone/showGetPhone"
					// 		})
					// 	}, 3000)
					// }
				} else {
					resolve(res.data);
				}
			},
			fail: err => {
				console.error(err);
				reject(err);
			}
		});
	});
}

// 上传请求（单个）
export function uploadFile(data) {
	console.log('getUserDetails');
	return request(MiniProgramApi.uploadFile, 'post', data)
}
// 查询公告列表
export function selectNoticeList(data) {
	console.log('selectNoticeList', token, openId);
	return request(MiniProgramApi.selectNoticeList, 'post', data)
}
//  根据id修改学校用户表
export function updateSchoolUser(data) {
	console.log('getUserDetails');
	return request(MiniProgramApi.updateSchoolUser, 'put', data)
}
// 查询报修分类列表下拉框
export function listSchoolRepairsClassDropdown(data) {
	console.log('listSchoolRepairsClassDropdown');
	return request(MiniProgramApi.listSchoolRepairsClassDropdown, 'get', data)
}
// 押金管理 扣费明细
export function consumer(data) {
	console.log('consumer');
	return request(MiniProgramApi.consumer, 'get', data)
}
// 获取用户信息
export function getWxUserInfo(data) {
	console.log('getWxUserInfo');
	return request(MiniProgramApi.getWxUserInfo + '?openId=' + openId, 'get', data)
}
//  根据学校用户id查询所属报修记录
export function findSRRecordBySchoolUserId(num) {
	console.log('findSRRecordBySchoolUserId');
	return request(MiniProgramApi.findSRRecordBySchoolUserId + num, 'post')
}
// 根据id查询宿舍表详情
export function findDormitoryById(data) {
	console.log('getUserDetails');
	return request(MiniProgramApi.findDormitoryById + data, 'get')
}
// 根据充值类型查询充值记录
export function findByMoneyTypeAndDataType(data) {
	console.log('getUserDetails');
	return request(MiniProgramApi.findByMoneyTypeAndDataType, 'get', data)
}
// 修改密码
export function changePassword(data) {
	console.log('getUserDetails');
	return request(MiniProgramApi.changePassword, 'put', data)
}
// 新增报修记录
export function addSRRecord(data) {
	console.log('getUserDetails');
	return request(MiniProgramApi.addSRRecord, 'post', data)
}
// 小程序注册方法
export function wxRegister(data) {
	console.log('getUserDetails');
	return request(MiniProgramApi.wxRegister, 'post', data)
}
// 小程序退出登录
export function wxLogout(data) {
	console.log('wxLogout');
	return request(MiniProgramApi.wxLogout, 'post', data)
}
// 小程序登录方法
export function wxLogin(data) {
	console.log('wxLogin');
	return request(MiniProgramApi.wxLogin, 'post', data)
}
// 根据openId获取token
export function getTokenByOpenId(data) {
	console.log('getTokenByOpenId');
	return request(MiniProgramApi.getTokenByOpenId, 'post', data)
}
// 获取openId
export function getOpenId(data) {
	console.log('getOpenId');
	return request(MiniProgramApi.getOpenId, 'post', data)
}
// 调用微信支付
export function callWxPay(data) {
	console.log('callWxPay');
	return request(MiniProgramApi.callWxPay, 'post', data)
}